<?php

class M_group extends CI_Model{
	
	function M_group(){
		parent::__construct();
	}
	
	function get_permission(){
		$query = "SELECT * FROM s_menus WHERE menu_parent<>0 AND menu_aktif='Y'";
		$result = $this->db->query($query);
		$nbrows = $result->num_rows();
		$query = "SELECT menu_id,menu_parent,menu_title,menu_position,1 as perm_read, 0 as perm_create,
		0 as perm_update, 0 as perm_delete FROM s_menus
		WHERE menu_parent=0 and menu_aktif='Y' ORDER By menu_position";
		$result = $this->db->query($query);
		
		$jsonresult = "";
		$data = array();
		if($nbrows>0){
			$i=0;
			foreach($result->result() as $row){
				$data[] = $row;
				
				$sql = "SELECT menu_id,menu_parent,menu_title,menu_position,0 as perm_read, 0 as perm_create,
				0 as perm_update, 0 as perm_delete FROM s_menus
				WHERE menu_parent='".$row->menu_id."' AND menu_aktif='Y' ORDER By menu_position";
				$rs_child=$this->db->query($sql);
				foreach($rs_child->result() as $rowchild){
					$data[] = $rowchild;
					
					/*$sql_perm="SELECT menu_id,menu_parent,menu_title,menu_position,perm_read,perm_create,perm_update,perm_delete
					FROM vu_s_permissions
					WHERE perm_menu='".$rowchild->menu_id."'
					and menu_aktif='Y'";
					$rs_perm=$this->db->query($sql_perm);
					if($rs_perm->num_rows()){
						$rowperm=$rs_perm->row();
						$data[] = $rowperm;
						$sql_perm="";
					}else
						$data[] = $rowchild;*/
				}
				$sql="";
				$i++;
			}
			return "{\"data\":" .json_encode($data). "}";
		} else {
			return "{\"data\":" .$jsonresult. "}";
		}
	}
	
	function get_permission_bygroup($group_id){
		$query = "SELECT * FROM s_menus WHERE menu_parent<>0 AND menu_aktif='Y'";
		$result = $this->db->query($query);
		$nbrows = $result->num_rows();
		$query = "SELECT menu_id,menu_parent,menu_title,menu_position,1 as perm_read, 0 as perm_create,
		0 as perm_update, 0 as perm_delete FROM s_menus
		WHERE menu_parent=0 and menu_aktif='Y' ORDER By menu_position";
		$result = $this->db->query($query);
	
		$jsonresult = "";
		$data = array();
		if($nbrows>0){
			$i=0;
			foreach($result->result() as $row){
				$data[] = $row;
	
				$sql = "SELECT menu_id,menu_parent,menu_title,menu_position,0 as perm_read, 0 as perm_create,
				0 as perm_update, 0 as perm_delete FROM s_menus
				WHERE menu_parent='".$row->menu_id."' AND menu_aktif='Y' ORDER By menu_position";
				$rs_child=$this->db->query($sql);
				foreach($rs_child->result() as $rowchild){
					$sql_perm="SELECT menu_id,menu_parent,menu_title,menu_position,perm_read,perm_create,perm_update,perm_delete
						FROM vu_s_permissions
					WHERE perm_group = ".$group_id." AND perm_menu='".$rowchild->menu_id."'
					and menu_aktif='Y'";
					$rs_perm=$this->db->query($sql_perm);
					if($rs_perm->num_rows()){
					$rowperm=$rs_perm->row();
					$data[] = $rowperm;
					$sql_perm="";
					}else
						$data[] = $rowchild;
				}
				$sql="";
				$i++;
			}
			return "{\"data\":" .json_encode($data). "}";
		} else {
			return "{\"data\":" .$jsonresult. "}";
		}
	}
	
	function group_list(){
		$query = "SELECT group_id, group_name, group_desc, group_active
		FROM s_usergroups";
			
		$result = $this->db->query($query);
		$nbrows = $result->num_rows();
		
		$jsonresult = "";
		$data = array();
		if($nbrows>0){
			$i=0;
			foreach($result->result() as $row){
				$data[$i] = $row;
				$i++;
			}
			return "{\"data\":" .json_encode($data). "}";
		} else {
			return "{\"data\":" .$jsonresult. "}";
		}
	}
	
	function create($group_name, $group_desc, $group_active, $array_perm_menu, $array_menu_parent){
		$sql = "SELECT group_name FROM s_usergroups WHERE group_name='".$group_name."'";
		$rs = $this->db->query($sql);
		if($rs->num_rows()>0){
			return 0;
		}else{
			$data = array(
					"group_name"=>$group_name,
					"group_desc"=>$group_desc,
					"group_active"=>$group_active
			);
			$this->db->insert('s_usergroups', $data);
			if($this->db->affected_rows()){
				$group_id = $this->db->insert_id();
				for ($i=0; $i<sizeof($array_perm_menu); $i++){
					$data_perm = array(
							"perm_group"=>$group_id,
							"perm_priv"=>'RCUD'
					);
					
					if($array_menu_parent[$i] <> 0){
						$sql_parent = "SELECT perm_group FROM s_permissions WHERE perm_group=".$group_id." AND perm_menu=".$array_menu_parent[$i];
						$rs_parent = $this->db->query($sql_parent);
						if($rs_parent->num_rows()<1){
							//add parent
							$data_perm["perm_menu"] = $array_menu_parent[$i];
							$this->db->insert('s_permissions', $data_perm);
							//add child
							$data_perm["perm_menu"] = $array_perm_menu[$i];
							$this->db->insert('s_permissions', $data_perm);
						}else{
							$data_perm["perm_menu"] = $array_perm_menu[$i];
							$this->db->insert('s_permissions', $data_perm);
						}
					}
				}
				
				return 1;
			}else{
				return 0;
			}
		}
	}
	
	function update($group_id, $group_name, $group_desc, $group_active, $array_perm_menu, $array_menu_parent){
		$data = array(
				"group_name"=>$group_name,
				"group_desc"=>$group_desc,
				"group_active"=>$group_active
		);
		$this->db->where('group_id', $group_id);
		$this->db->update('s_usergroups', $data);
		
		//delete all permissions
		$sqld_perm = "DELETE FROM s_permissions WHERE perm_group=".$group_id;
		$this->db->query($sqld_perm);
		for ($i=0; $i<sizeof($array_perm_menu); $i++){
			$data_perm = array(
					"perm_group"=>$group_id,
					"perm_priv"=>'RCUD'
			);
			
			if($array_menu_parent[$i] <> 0){
				$sql_parent = "SELECT perm_group FROM s_permissions WHERE perm_group=".$group_id." AND perm_menu=".$array_menu_parent[$i];
				$rs_parent = $this->db->query($sql_parent);
				if($rs_parent->num_rows()<1){
					//add parent
					$data_perm["perm_menu"] = $array_menu_parent[$i];
					$this->db->insert('s_permissions', $data_perm);
					//add child
					$data_perm["perm_menu"] = $array_perm_menu[$i];
					$this->db->insert('s_permissions', $data_perm);
				}else{
					$data_perm["perm_menu"] = $array_perm_menu[$i];
					$this->db->insert('s_permissions', $data_perm);
				}
			}
		}
		
		return 1;
	}
	
}

?>